//
//  JZBDateViewController.m
//  JZB
//
//  Created by  on 2022/8/31.
//

#import "JZBDateViewController.h"
@interface JZBDateViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UILabel *titleLbel;
@property (strong, nonatomic) IBOutlet UIPickerView *pickview;
@property (nonatomic, strong) NSDateComponents *date;
@property (nonatomic, strong) NSDateComponents *selectDate;
@end

@implementation JZBDateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.stamp == 0) {
        self.stamp = [[NSDate currentTimeStamp:@"yyyy-MM-dd HH:mm:ss"] integerValue];
    }}

- (void)setStamp:(NSInteger)stamp{
    _stamp = stamp;
    if (self.pickview == nil) {
        return;
    }
    self.date = [NSDate dateOfStamp:self.stamp];
    self.selectDate = [NSDate dateOfStamp:self.stamp];
    [self.pickview reloadAllComponents];
    [self.pickview selectRow:self.date.year inComponent:0 animated:NO];
    [self.pickview selectRow:self.date.month - 1 inComponent:1 animated:NO];
    [self.pickview selectRow:self.date.day - 1 inComponent:2 animated:NO];
}

- (int)stamp:(int)day{
    NSString *str = [NSString stringWithFormat:@"%d/%d/%d",(int)self.selectDate.year,(int)self.selectDate.month,day];
    return [[NSDate tranToStamp:str :@"yyyy/MM/dd"] intValue];
}
- (IBAction)actionforBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }else if (sender.tag == 202){
        [self dismissViewControllerAnimated:NO completion:nil];
        if (self.selectBlock) {
            self.selectBlock([self stamp:(int)self.selectDate.day]);
        }
    }
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 30;
    }else if (component == 1){
        return  12;
    }else{
        return [NSDate numOfMonth:[self stamp:1]];
    }
}
#pragma mark - UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return  100;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.selectDate.year = self.date.year - row;
    }else if(component == 1){
        self.selectDate.month = row + 1;
    }else{
        self.selectDate.day = row + 1;
    }
    [pickerView reloadComponent:2];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *lbl = [UILabel new];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.textHexColor = @"#333333";
    if (component == 0) {
        lbl.text = [NSString stringWithFormat:LS(@"%d年"),(int)self.date.year - (int)row];
    }else if(component == 1){
        lbl.text = [NSString stringWithFormat:LS(@"%d月"),(int)row + 1];
    }else{
        lbl.text = [NSString stringWithFormat:LS(@"%d日"),(int)row + 1];
    }
    return lbl;
}

@end
